package com.acknight.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.acknight.constant.CommitComstant;
import com.acknight.dto.Result;
import com.acknight.entity.DetailBlogComment;
import com.acknight.entity.User;
import com.acknight.mapper.DetailBlogCommentMapper;
import com.acknight.service.DetailBlogCommentService;
import com.acknight.service.IUserService;
import com.acknight.vo.CommentVo;
import com.acknight.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DetailBlogCommentServiceImpl extends ServiceImpl<DetailBlogCommentMapper, DetailBlogComment> implements DetailBlogCommentService {

    @Resource
    private IUserService userService;

    @Override
    public Result commentList(Long articleId, Integer pageNum, Integer pageSize) {
        // 查询对应文章的跟评论
        LambdaQueryWrapper<DetailBlogComment> queryWrapper = new LambdaQueryWrapper<>();


        queryWrapper.eq(DetailBlogComment::getBlogId, articleId);
        // 根评论 rootId为-1
        queryWrapper.eq(DetailBlogComment::getRootId, CommitComstant.COMMENT_ROOT_ID);

        // 分页查询
        Page<DetailBlogComment> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());

        // 查询所有根评论的子评论
        for (CommentVo commentVo : commentVoList) {
            // 查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());

            commentVo.setChildren(children);
        }

        return Result.ok(new PageVo(commentVoList, page.getTotal()));
    }

    @Override
    public Result addCommit(DetailBlogComment detailBlogComment) {
        if (detailBlogComment.getCreateBy() == null) {
            return Result.fail("数据为空!");
        }
        detailBlogComment.setCreateTime(new Date());
        detailBlogComment.setUpdateTime(new Date());
        detailBlogComment.setUpdateBy(detailBlogComment.getCreateBy());
        save(detailBlogComment);
        return Result.ok();
    }

    private List<CommentVo> toCommentVoList(List<DetailBlogComment> list) {
        List<CommentVo> commentVos = BeanUtil.copyToList(list, CommentVo.class);
        // 遍历vo集合
        for (CommentVo commentVo : commentVos) {
            // 通过createBy查询用户的昵称并赋值
            User user = userService.getById(commentVo.getCreateBy());
            String nickName = user.getNickName();
            commentVo.setUsername(nickName);
            commentVo.setAvatar(user.getIcon());
            // 通过toCommentUserId查询用户的昵称并赋值
            // 通过toCommentUserId不为-1才进行查询
            if (commentVo.getToCommentUserId() != -1) {
                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentVos;
    }

    /**
     * 根据根评论id查询对应的子评论集合
     *
     * @param id 根评论id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<DetailBlogComment> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(DetailBlogComment::getRootId, id);
        queryWrapper.orderByAsc(DetailBlogComment::getCreateTime);

        List<DetailBlogComment> comments = list(queryWrapper);

        List<CommentVo> commentVos = toCommentVoList(comments);

        return commentVos;
    }
}
